VERSION 5.00
Begin VB.Form frmAdd 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Add users"
   ClientHeight    =   4755
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   5550
   LinkTopic       =   "Form2"
   MaxButton       =   0   'False
   MDIChild        =   -1  'True
   MinButton       =   0   'False
   ScaleHeight     =   4755
   ScaleWidth      =   5550
   Begin VB.ComboBox cboOff 
      Height          =   315
      ItemData        =   "frmAdd.frx":0000
      Left            =   840
      List            =   "frmAdd.frx":0013
      TabIndex        =   15
      Top             =   3120
      Width           =   1815
   End
   Begin VB.Frame Frame1 
      Caption         =   "Permission"
      Height          =   735
      Left            =   120
      TabIndex        =   11
      Top             =   2280
      Width           =   5295
      Begin VB.OptionButton Option5 
         Caption         =   "GM"
         Height          =   375
         Left            =   4200
         TabIndex        =   18
         Top             =   240
         Width           =   855
      End
      Begin VB.OptionButton Option4 
         Caption         =   "MB"
         Height          =   375
         Left            =   3360
         TabIndex        =   17
         Top             =   240
         Width           =   735
      End
      Begin VB.OptionButton Option3 
         Caption         =   "Administrator"
         Height          =   375
         Left            =   2040
         TabIndex        =   14
         Top             =   240
         Width           =   1335
      End
      Begin VB.OptionButton Option2 
         Caption         =   "Reporter"
         Height          =   375
         Left            =   960
         TabIndex        =   13
         Top             =   240
         Width           =   1095
      End
      Begin VB.OptionButton Option1 
         Caption         =   "User"
         Height          =   375
         Left            =   240
         TabIndex        =   12
         Top             =   240
         Value           =   -1  'True
         Width           =   735
      End
   End
   Begin VB.CommandButton cmdAdd 
      Caption         =   "&Add"
      Height          =   375
      Left            =   1800
      TabIndex        =   7
      Top             =   3840
      Width           =   1455
   End
   Begin VB.CommandButton cmdCancel 
      Caption         =   "&Cancel"
      Height          =   375
      Left            =   3720
      TabIndex        =   6
      Top             =   3840
      Width           =   1455
   End
   Begin VB.CommandButton cmdOK 
      Caption         =   "&Ok"
      Height          =   375
      Left            =   2160
      TabIndex        =   5
      Top             =   5160
      Width           =   1335
   End
   Begin VB.TextBox txtConfirm 
      Height          =   375
      IMEMode         =   3  'DISABLE
      Left            =   2400
      PasswordChar    =   "*"
      TabIndex        =   4
      Top             =   1680
      Width           =   2775
   End
   Begin VB.TextBox txtPass 
      Height          =   375
      IMEMode         =   3  'DISABLE
      Left            =   2400
      PasswordChar    =   "*"
      TabIndex        =   3
      Top             =   1200
      Width           =   2775
   End
   Begin VB.TextBox txtUser 
      Height          =   375
      Left            =   2400
      TabIndex        =   2
      Top             =   720
      Width           =   2775
   End
   Begin VB.TextBox txtTen 
      Height          =   375
      Left            =   2400
      TabIndex        =   1
      Top             =   240
      Width           =   2775
   End
   Begin VB.Label Label3 
      Caption         =   "Office"
      Height          =   255
      Left            =   240
      TabIndex        =   16
      Top             =   3120
      Width           =   615
   End
   Begin VB.Label Label9 
      BackStyle       =   0  'Transparent
      Caption         =   "User name:"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   120
      TabIndex        =   10
      Top             =   720
      Width           =   1095
   End
   Begin VB.Label Label2 
      BackStyle       =   0  'Transparent
      Caption         =   "Password:"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   120
      TabIndex        =   9
      Top             =   1200
      Width           =   1455
   End
   Begin VB.Label Label4 
      BackStyle       =   0  'Transparent
      Caption         =   "Confirm new password:"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   120
      TabIndex        =   8
      Top             =   1680
      Width           =   2175
   End
   Begin VB.Label Label1 
      BackStyle       =   0  'Transparent
      Caption         =   "Staff Name:"
      Height          =   255
      Left            =   120
      TabIndex        =   0
      Top             =   240
      Width           =   975
   End
End
Attribute VB_Name = "frmAdd"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim F As Boolean
Dim T As Boolean
Dim sql As String
Public UserName As String
Dim rs As New ADODB.Recordset

Private Sub cmdAdd_Click()
    sql = "Select * from tbl_pass where username = '" & txtUser & "'"
    If Trim(txtTen.Text) = "" Then
        MsgBox "Staff name box must be filled in"
        Exit Sub
   End If
    
   If Trim(txtUser.Text) = "" Then
        MsgBox "Username box must be filled in"
        Exit Sub
   End If
   If Trim(txtPass.Text) = "" Then
        MsgBox "Password box must be filled in"
        Exit Sub
   End If
   
       
    If cmdAdd.Caption = "Save change" Then
            If txtPass.Text = txtConfirm.Text Then
                  sql = "update  tbl_pass set name='" & txtTen.Text & "', UserName='" _
                  & txtUser.Text & "',pass='" & txtPass.Text & "',permit='"
                  If Option1.Value = True Then
                      sql = sql & "U'"
                  ElseIf Option2.Value = True Then
                      sql = sql & "R'"
                  ElseIf Option3.Value = True Then
                      sql = sql & "A'"
                  ElseIf Option4.Value = True Then
                      sql = sql & "B'"
                 ElseIf Option5.Value = True Then
                      sql = sql & "G'"
                  End If
                  
                  sql = sql & ",office='" & cboOff.Text & "' "
                  sql = sql & " where UserName ='" & UserName & "'"
                  adoConnection.Execute (sql)
                  MsgBox "Save Successfully"
                  cmdAdd.Caption = "Add"
                  UserName = ""
              Else
                  MsgBox "Invalid password"
                  rs.Close
                  Exit Sub
              End If
    Else
    
          Set rs = adoConnection.Execute(sql)
          If Not rs.EOF Then
              MsgBox "This username exists"
          Else
              If txtPass.Text = txtConfirm.Text Then
                  sql = "Insert into tbl_pass values('" & txtTen.Text & "','" _
                  & txtUser.Text & "','" & txtPass.Text & "','"
                  If Option1.Value = True Then
                      sql = sql & "U'"
                  ElseIf Option2.Value = True Then
                      sql = sql & "R'"
                  ElseIf Option3.Value = True Then
                      sql = sql & "A'"
                ElseIf Option4.Value = True Then
                      sql = sql & "B'"
                 ElseIf Option5.Value = True Then
                      sql = sql & "G'"
                  End If
                   sql = sql & ",'" & cboOff.Text & "')"
                  adoConnection.Execute (sql)
                  MsgBox "Add Successfully"
              Else
                  MsgBox "Invalid password"
                  rs.Close
                  Exit Sub
              End If
            End If
        
    End If
    'rs.Close
    txtTen.Text = ""
    txtUser.Text = ""
    txtPass.Text = ""
    txtConfirm.Text = ""
    Option1.Value = True
    frmUserManager.displayUser
End Sub

Private Sub cmdCancel_Click()
Dim i As Integer
If (InfoFlag = True) And (cmdOK.Enabled = True) Then
    i = MsgBox("Do you want to save info?", vbOKCancel)
    If i = vbOK Then saveinfo
End If
        Unload Me
        frmUserManager.Show
End Sub
Private Sub cmdOK_Click()
   If Trim(txtTen.Text) = "" Then
        MsgBox "Staff name box must be filled in"
        Exit Sub
   End If
    
   If Trim(txtUser.Text) = "" Then
        MsgBox "Username box must be filled in"
        Exit Sub
   End If
   If Trim(txtPass.Text) = "" Then
        MsgBox "Password box must be filled in"
        Exit Sub
   End If
   
      
    sql = "Select * from tbl_pass where username = '" & txtUser & "'"
    Set rs = adoConnection.Execute(sql)
    If Not rs.EOF Then
        MsgBox "This username exists"
    Else
        If txtPass.Text = txtConfirm.Text Then
            sql = "Insert into tbl_pass values('" & txtTen.Text & "','" _
            & txtUser.Text & "','" & txtPass.Text & "','"
            If Option1.Value = True Then
                sql = sql & "T')"
            Else
                sql = sql & "TR')"
            End If
            cn.Execute (sql)
            frmUserManager.displayUser
            Unload Me
        Else
            MsgBox "Invalid password"
        End If
            
        
    End If
End Sub

Private Sub Form_Load()
Me.Top = Screen.Height / 2 - (Me.Height / 1.5)
    Me.Left = Screen.Width / 2 - (Me.Width / 2)
    cboOff.Text = stroffice
    DisplayInfo
    
End Sub

Private Sub Form_Unload(Cancel As Integer)
If InfoFlag = True Then InfoFlag = False
frmUserManager.displayUser
End Sub

Private Sub txtChucvu_Change()
If InfoFlag = True Then cmdOK.Enabled = True

End Sub
Private Sub txtDienthoai_Change()
If InfoFlag = True Then cmdOK.Enabled = True

End Sub

Private Sub txtPhongban_Change()

If InfoFlag = True Then cmdOK.Enabled = True
End Sub

Private Sub txtTen_Change()

If InfoFlag = True Then cmdOK.Enabled = True
End Sub

'----------- USER DEFINE -----------------

Private Function DisplayInfo()
    Dim sql As String
    Dim rs As New ADODB.Recordset
    If Trim(UserName) <> "" Then
        sql = "Select * from tbl_pass where username ='" & UserName & "'"
        Set rs = adoConnection.Execute(sql)
        If Not rs.EOF Then
            If Not IsNull(rs!Name) Then
                txtTen.Text = rs!Name
            End If
            If Not IsNull(rs!UserName) Then
                txtUser.Text = rs!UserName
            End If
            txtPass.Text = "*****"
            txtConfirm.Text = "*****"
            cboOff.Text = rs!Office
            If Not IsNull(rs!permit) Then
                If UCase(Trim(rs!permit)) = "R" Then
                    Option2.Value = True
                ElseIf UCase(Trim(rs!permit)) = "A" Then
                    Option3.Value = True
                ElseIf UCase(Trim(rs!permit)) = "B" Then
                    Option4.Value = True
                ElseIf UCase(Trim(rs!permit)) = "G" Then
                    Option5.Value = True
                Else
                    Option1.Value = True
                End If
            End If
        End If
        cmdAdd.Caption = "Save change"
    End If
   ' rs.Close
    Set rs = Nothing
End Function
Private Function savenew()
Dim db As ADODB.Connection
Dim rst As ADODB.Recordset
Dim ok As Boolean

Set db = New ADODB.Connection
db.Provider = "Microsoft.Jet.OLEDB.3.51"
db.Open App.Path & "\pass.dat"

Set rst = New ADODB.Recordset
rst.Open "tblUser", db, adOpenDynamic, adLockOptimistic
ok = True

With rst
    .MoveFirst
    Do Until .EOF
        If ![Name] = Me![txtUser] Then ok = False
        .MoveNext
    Loop
    If ok = False Then
        MsgBox "This username is exists", , "User Dublication"
        txtUser.SetFocus
    Else
        If txtPass.Text = txtConfirm.Text Then
            .MoveLast
            .AddNew
            !Name = txtUser
            !Password = txtPass
            !Dem = 1
            .Update
        Else
            MsgBox "Wrong password, please enter the right one", , "Wrong Password"
            Exit Function
            txtConfirm.SetFocus
        End If
    End If
End With

Set rst = New ADODB.Recordset
rst.Open "tblInfo", db, adOpenDynamic, adLockOptimistic
With rst
    .MoveLast
    .AddNew
    ![User] = Me![txtUser]
    ![ten] = Me![txtTen]
    ![dienthoai] = Me![txtDienthoai]
    ![chucvu] = Me![txtChucvu]
    ![phongban] = Me![txtPhongban]
    .Update
End With
Set rst = Nothing
Unload Me
Form1.Show

End Function
Private Function saveinfo()
Dim db As ADODB.Connection
Dim rst As ADODB.Recordset
Dim ok As Boolean

Set db = New ADODB.Connection
db.Provider = "Microsoft.Jet.OLEDB.3.51"
db.Open App.Path & "\pass.dat"


Set rst = New ADODB.Recordset
rst.Open "tblInfo", db, adOpenDynamic, adLockOptimistic
With rst
    .MoveFirst
    Do Until .EOF
        If ![User] = Me![txtUser] Then
            ![ten] = Me![txtTen]
            ![dienthoai] = Me![txtDienthoai]
            ![chucvu] = Me![txtChucvu]
            ![phongban] = Me![txtPhongban]
            .Update
        End If
        .MoveNext
    Loop
End With
Set rst = Nothing
Unload Me
Form1.Show

End Function

